package com.boot.config

import com.boot.core.interceptor.AuthInterceptor
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer


@Configuration
class WebMvcConfig : WebMvcConfigurer {

    @Bean
    fun authInterceptor(): AuthInterceptor {
        return AuthInterceptor()
    }

    @Bean
    fun threadPoolTaskExecutor(): ThreadPoolTaskExecutor {
        return ThreadPoolTaskExecutor().apply {
            corePoolSize = 5
            keepAliveSeconds = 200
            maxPoolSize = 10
        }
    }

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(authInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/police/login", "/error", "/test/**")
                .excludePathPatterns("/admin/monitor/**")
                .excludePathPatterns("/**/**/rpc/**")
                .excludePathPatterns("/rpc/**")
                .excludePathPatterns("/police/verifyBillUnify")
    }

    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        super.addResourceHandlers(registry)
        val os = System.getProperty("os.name")

        if (os.toLowerCase().startsWith("win")) {
            registry.addResourceHandler("/app/img/**").addResourceLocations("file:" + System.getProperty("user.dir") + "\\src\\main\\resources\\static\\img\\")
        } else {//linux
            registry.addResourceHandler("/app/img/**").addResourceLocations("file:/logs/")
        }
    }
}